<?php

namespace app\index\controller;

use think\Controller;
use think\Request;

class base extends Controller
{

    public $template;
    public function _initialize()
    {
      $this->template = '../template/pc/';
    }

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        //全站信息
        $system = model('system')->getnoteall('system');

        //头部导航
//        $navs = model('nav')->catenav(['pid'=>'0']);

        //底部导航
        $nav = model('nav')->where('pid', '0')->order('sort ASC,id ASC')->limit(10)->select();

        //在线帮助
        $online = model('online')->select();

        //幻灯片
        $where = '';
        $navid = $this->request->param('navid');
        !empty($navid) ? $where['type'] = ['like', '%,' . $navid . ',%'] : '';
        $carousel = model('carousel')->where($where)->limit(1)->select();

        $this->assign([
            'carousel' => $carousel,
            'online' => $online,
//            'navs' => $navs,
            'nav' => $nav,
            'system' => $system,
        ]);
    }


    protected $beforeActionList = [
        'right' =>  ['only'=>'index,detailed'],
    ];

    public function index()
    {
        $navid = $this->request->param('navid');

        if (!$navid) {
            $this->error('参数错误');
        }
        /*栏目详情start*/
        $cateInfo = db('nav')->where('id', $navid)->find();
        if (!$cateInfo) {
            $this->error('栏目不存在');
        }
        /*栏目详情end*/

        $this->assign('cateInfo', $cateInfo);

        /*模板start*/
        if ($cateInfo['type'] == 'one') {
            /*文章列表end*/
            return $this->fetch($this->template . $cateInfo['temppage']);
        } else {
            /*文章列表start*/
//            $list = db($cateInfo['model'])->alias('a')->join('__NAV__ c', 'c.id= a.cid')->field('a.*,c.name')->where('a.cid', $navid)->order('a.id desc')->paginate();
            $list = model($cateInfo['model'])->where('cid', $navid)->paginate();

            $this->assign('list', $list);
            /*文章列表end*/
            return $this->fetch($this->template . $cateInfo['templist']);
        }
        /*模板end*/
    }



    public function detailed($id)
    {
        //显示内容
        $model = $this->request->controller();

        $data = model($model)->find($id);

        $cateInfo = db('nav')->where('id', $data['cid'])->find();

        //更多页面
        !empty($data['cid']) ? $datas['cid'] = $data['cid'] : '';
        $next = model($model)->getnext($id, $datas);
        $this->assign('data', $data);
        $this->assign('cateInfo', $cateInfo);
        $this->assign('next', $next);
        return $this->fetch($this->template . $cateInfo['temparticle']);
    }

    //左边导航
    public function right()
    {

        $pid = model('nav')->where('id',$this->request->param('navid'))->value('pid');
        $pid = !empty($pid)?$pid:$this->request->param('navid');
        $reight_nav = model('nav')
            ->where('pids', 'like', '%,' . $pid . ',%')
            ->order('sort asc,id ASC')
            ->select();

        $this->assign('reight_nav', $reight_nav);

    }


}
